package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.gc.TerminalInfo;
import com.egame.manage.biz.server.op.OperationActivityServiceImpl;
import com.egame.manage.biz.domain.TerminalBean;

public class TerminalServiceDao extends BaseDao {
	
	private static final String STR_INSERT_TERMINAL_INFO = "terminal_name,terminal_group_id,terminal_type,ua_code,short_ua,"
			+ "feature_code,chip_code,screen_px,os_name,os_version,browser,is_touch,is_wap,"
			+ "is_jar,is_jad,is_cab,is_apk,remark,data_status,operator_id,"
			+ "insert_time,update_time,wap_type,manufacturer_id";
	
	public TerminalServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}	

	public int setTerminalInfo(TerminalInfo terminalInfo)
			throws ExceptionCommonBase {
		if(terminalInfo != null){
			terminalInfo.setDataStatus(1);
			String sql = "insert into t_terminal ("
					+ STR_INSERT_TERMINAL_INFO
					+ ") values ('%s',%d,%d,'%s','%s','%s','%s','%s','%s','%s','%s',%d,%d,%d,%d,%d,%d,'%s',%d,%d,now(),now(),%d,%d)";
			sql = String.format(sql, terminalInfo
					.getTerminalName(), terminalInfo.getTerminalGroupId(),
					terminalInfo.getTerminalType().value(), terminalInfo
							.getUACode(), terminalInfo.getShortUA(), terminalInfo
							.getFeatureCode(), terminalInfo.getChipCode(),
					terminalInfo.getScreenPx(), terminalInfo.getOsName(),
					terminalInfo.getOsVersion(), terminalInfo.getBrowser(),
					terminalInfo.getIsTouch(), terminalInfo.getIsWap(),
					terminalInfo.getIsJar(), terminalInfo.getIsJad(), terminalInfo
							.getIsCad(), terminalInfo.getIsApk(), terminalInfo
							.getRemark(), terminalInfo.getDataStatus(),
					terminalInfo.getOperatorId(),
					terminalInfo.getWapType().value(), terminalInfo
							.getManufacturerId());
			System.out.println(sql);
			return this.getIdentityId(sql);
		}
		return -1;
	}
	
//	terminal_id,terminal_name,terminal_group_id,terminal_type,ua_code,short_ua,"
//	"feature_code,chip_code,screen_px,os_name,os_version,browser,is_touch,is_wap,"
//	"is_jar,is_jad,is_cab,is_apk,remark,data_status,operator_id,"
//	"insert_time,update_time,wap_type,manufacturer_id
	public int updateTerminalBean(TerminalBean terminalInfo) throws ExceptionCommonBase{
		if(terminalInfo != null){
			String sql = "update t_terminal set ";
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getTerminalName())){
					sql += " terminal_name = " +SqlUtils.QuataStr(terminalInfo.getTerminalName()) + ",";
				}				
				if(terminalInfo.getTerminalType() != null){
					sql += " terminal_type = " + terminalInfo.getTerminalType() + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getUACode())){
					sql += " ua_code = " + SqlUtils.QuataStr(terminalInfo.getUACode()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getShortUA())){
					sql += " short_ua = " + SqlUtils.QuataStr(terminalInfo.getShortUA()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getFeatureCode())){
					sql += " feature_code = " + SqlUtils.QuataStr(terminalInfo.getFeatureCode()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getChipCode())){
					sql += " chip_code = " + SqlUtils.QuataStr(terminalInfo.getChipCode()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getScreenPx())){
					sql += " screen_px = " + SqlUtils.QuataStr(terminalInfo.getScreenPx()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getOsName())){
					sql += " os_name = " + SqlUtils.QuataStr(terminalInfo.getOsName()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getOsVersion())){
					sql += " os_version = " + SqlUtils.QuataStr(terminalInfo.getOsVersion()) + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getBrowser())){
					sql += " browser = " + SqlUtils.QuataStr(terminalInfo.getBrowser()) + ",";
				}
				if(terminalInfo.getIsTouch() != null){
					sql += " is_touch = " + terminalInfo.getIsTouch() + ",";
				}
				if(terminalInfo.getIsJar() != null){
					sql += " is_jar = " + terminalInfo.getIsJar() + ",";
				}
				if(terminalInfo.getIsJad() != null){
					sql += " is_jad = " + terminalInfo.getIsJad() + ",";
				}
				if(terminalInfo.getIsCad() != null){
					sql += " is_cab = " + terminalInfo.getIsCad() + ",";
				}
				if(terminalInfo.getIsApk() != null){
					sql += " is_apk = " + terminalInfo.getIsApk() + ",";
				}
				if(!Utils.stringIsNullOrEmpty(terminalInfo.getRemark())){
					sql += " remark = " + SqlUtils.QuataStr(terminalInfo.getRemark()) + ",";
				}
				
				sql += " update_time = now() where terminal_id = " + terminalInfo.getTerminalId();
			System.out.println(sql);
			return this.executeUpdate(sql);
		}
		return -1;
	}
	public int deleteTerminalInfo(int terminalId) throws ExceptionCommonBase {
		String sql = "update t_terminal set data_status = '0' where terminal_id = " + terminalId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public List<Integer> listTerminalIds()throws ExceptionCommonBase{
		String sql = "select terminal_id from t_terminal where data_status = 1";
		return this.getListInteger(sql);
	}
	
	public List<Integer> listTerminalIds(int gId,long fileId,int page,int rowsOfPage)throws ExceptionCommonBase{
		String sql = "select terminal_id from t_terminal_game_file_link" +
				" where g_id = " + gId + 
				" and file_id = " + fileId +
				" limit " + page * rowsOfPage + "," + rowsOfPage;
		System.out.println(sql);
		return this.getListInteger(sql);
	}

	public List<Integer> listTerminalIds(int gId,long fileId, int manufactureId, int adapted,int page,int rowsOfPage)throws RemoteException{
		String sql = "select terminal_id from t_terminal where data_status = 1 ";
		if(manufactureId>0){
			sql += "and manufacturer_id = " + manufactureId;
		}
		List<Integer> terminals = this.listTerminalIds(fileId, gId);
		if(terminals == null || terminals.size()==0){
			terminals = new ArrayList<Integer>();
			terminals.add(0);
		}
		if(adapted == 1){//未适配
			sql += " AND terminal_id IN (" + Utils.toString(this.listTerminalByNotAdaptedByGid(gId)) + ")";
		}else if(adapted == 2){
			sql += " AND terminal_id IN (" + Utils.toString(terminals) + ")";
		}else{
			sql += " AND terminal_id IN (" + Utils.toString(this.listTerminalByNotAdaptedByFid(gId,fileId)) + ")";
		}
		sql += " limit " + page * rowsOfPage + "," + rowsOfPage;
		System.out.println(sql);
		return this.getListInteger(sql);
	}

	public int getTerminalCount(int gId,long fileId, int manufactureId, int adapted)throws RemoteException{
		String sql = "select count(*) from t_terminal where data_status = 1 ";
		if(manufactureId>0){
			sql += "and manufacturer_id = " + manufactureId;
		}
		List<Integer> terminals = this.listTerminalIds(fileId, gId);
		if(terminals == null || terminals.size()==0){
			terminals = new ArrayList<Integer>();
			terminals.add(0);
		}
		if(adapted == 1){//未适配
			sql += " AND terminal_id IN (" + Utils.toString(this.listTerminalByNotAdaptedByGid(gId)) + ")";
		}else if(adapted == 2){
			sql += " AND terminal_id IN (" + Utils.toString(terminals) + ")";
		}else{
			sql += " AND terminal_id IN (" + Utils.toString(this.listTerminalByNotAdaptedByFid(gId,fileId)) + ")";
		}
		System.out.println(sql);
		return this.getInt(sql);
	}

	public int terminalCount(int gId,long fileId)throws ExceptionCommonBase{
		String sql = "select count(*) from t_terminal_game_file_link" +
				" where g_id = " + gId + 
				" and file_id = " + fileId;
		System.out.println(sql);
		return this.getInt(sql);
	}
		
	public List<Integer> listTerminalIdsByAdapted(int gId,long fileId)throws ExceptionCommonBase{
		String sql = "select terminal_id from t_terminal_game_file_link where g_id = " + gId + " and file_id = " + fileId;
		System.out.println(sql);
		return this.getListInteger(sql);
	}
	
	public List<Integer> listTerminalByNotAdaptedByGid(int gId)throws RemoteException{
		String sql = "select terminal_id from t_terminal_game_file_link where g_id = " + gId;		
		List<Integer> terminalIds =  this.getListInteger(sql);
		if(terminalIds.size() == 0) terminalIds.add(0);
		sql = "select terminal_id from t_terminal where data_status = 1 and terminal_id not in (" + Utils.toString(terminalIds) + ")";
		System.out.println(sql);
		return this.getListInteger(sql);
	}

	public List<Integer> listTerminalByNotAdaptedByFid(int gid,long fid)throws RemoteException{
		String sql = "select terminal_id from t_terminal_game_file_link where file_id != " + fid + " and g_id = " + gid;		
		List<Integer> terminalIds =  this.getListInteger(sql);
		if(terminalIds.size() == 0) terminalIds.add(0);
		sql = "select terminal_id from t_terminal where data_status = 1 and terminal_id not in (" + Utils.toString(terminalIds) + ")";
		System.out.println(sql);
		return this.getListInteger(sql);
	}
	
	public List<Integer> listTerminalByNotAdapted(int activityid)throws RemoteException{
		List<Integer> terminalIds = OperationActivityServiceImpl.getInstance().listTerminalIds(activityid);
		if(terminalIds.size() == 0) terminalIds.add(0);
		String sql = "select terminal_id from t_terminal where data_status = 1 and terminal_id not in (" + Utils.toString(terminalIds) + ")";
		System.out.println(sql);
		return this.getListInteger(sql);
	}

	public int setTerminalFileInfo(int gid, long fileid, int terminalId,int terminalGroupId)throws ExceptionCommonBase {
		String sql = "insert into t_terminal_game_file_link(g_id,terminal_id,file_id,terminal_group_id)values("
				+ gid + ","
				+ terminalId + ","
				+ fileid + ","
				+ terminalGroupId + ")";
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public List<Integer> listTerminalIds(int manufactureId)throws RemoteException{
		
		String sql = "select terminal_id from t_terminal where 1=1 ";
		if(manufactureId>0){
			sql += " and manufacturer_id = " + manufactureId;
		}
		System.out.println(sql);
		return this.getListInteger(sql);
	}
	
	public List<String> listTerminalNames()  throws RemoteException{
		String sql = "select distinct terminal_name from t_terminal";
		return this.getListString(sql);
	}
	
	public List<String> listOsNames()  throws RemoteException{
		String sql = "select distinct os_name from t_terminal";
		return this.getListString(sql);
	}
	
	public List<String> listScreenPxs()  throws RemoteException{
		String sql = "select distinct screen_px from t_terminal";
		return this.getListString(sql);
	}
	
	public Map<String,Object> listTerminalIdAndShortUa(String TerName,String OsName,String screenPx,String shortUa) throws RemoteException{
		String sql="select terminal_id,short_ua from t_terminal where 1=1 ";
		if(!Utils.stringIsNullOrEmpty(TerName)){
			sql += " and terminal_name = " +SqlUtils.QuataStr(TerName);
		}	
		if(!Utils.stringIsNullOrEmpty(OsName)){
			sql += " and os_name = " +SqlUtils.QuataStr(OsName);
		}
		if(!Utils.stringIsNullOrEmpty(screenPx)){
			sql += " and screen_px = " +SqlUtils.QuataStr(screenPx);
		}
		if(!Utils.stringIsNullOrEmpty(shortUa)){
			sql += " and short_ua like " +SqlUtils.QuataLikeStr(shortUa.trim());
		}
		System.out.println(sql);
		List<Map<String,Object>> maps= this.getListKeyValue(sql);
		Map<String,Object> result = new HashMap<String,Object>();
		for(Map<String,Object> map:maps) {
			result.putAll(map);
		}
		return result;
	}
	
	public List<Integer> listTerminalIds(TerminalInfo bean,int page,int rowsOfPage)throws ExceptionCommonBase{
		String sql = "select terminal_id from t_terminal where data_status = 1 ";
		if(bean != null){
			if(bean.getManufacturerId()>0){
				sql += " and manufacturer_id = " + bean.getManufacturerId();
			}
			if(!Utils.stringIsNullOrEmpty(bean.getShortUA())){
				sql += " and short_ua like " + SqlUtils.QuataLikeStr(bean.getShortUA());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getUACode())){
				sql += " and ua_code like " + SqlUtils.QuataLikeStr(bean.getUACode());
			}
			if((!Utils.stringIsNullOrEmpty(bean.getScreenPx()))&&(!(bean.getScreenPx().equalsIgnoreCase("-1")))){
				sql += " and screen_px like " + SqlUtils.QuataLikeStr(bean.getScreenPx());
			}
		}
		sql += " limit " + page * rowsOfPage + "," + rowsOfPage;
		return this.getListInteger(sql);
	}

	public int getTerminalCount(TerminalInfo bean)throws ExceptionCommonBase{
		String sql = "select count(*) from t_terminal where data_status = 1 ";
		if(bean != null){
			if(bean.getManufacturerId()>0){
				sql += " and manufacturer_id = " + bean.getManufacturerId();
			}
			if(!Utils.stringIsNullOrEmpty(bean.getShortUA())){
				sql += " and short_ua like " + SqlUtils.QuataLikeStr(bean.getShortUA());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getUACode())){
				sql += " and ua_code like " + SqlUtils.QuataLikeStr(bean.getUACode());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getScreenPx())){
				sql += " and screen_px like " + SqlUtils.QuataLikeStr(bean.getScreenPx());
			}
		}
		return this.getInt(sql);
	}
	
	public boolean isTerminalExist(String uacode) throws ExceptionCommonBase{
		if(!Utils.stringIsNullOrEmpty(uacode)){
			String sql = "select count(*) from t_terminal where ua_code = " + SqlUtils.QuataStr(uacode);
			return this.getInt(sql)>0;
		}
		return false;
	}

	public int deleteAdaptedTerminal(int gid,long fileId,int terminalId)throws ExceptionCommonBase{
		String sql = "delete from t_terminal_game_file_link" +
				" where g_id = " + gid +
				" and file_id = " + fileId + 
				" and terminal_id = " + terminalId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public int deleteAdaptedTerminal(int gid,long fileId)throws ExceptionCommonBase{
		String sql = "delete from t_terminal_game_file_link" +
				" where g_id = " + gid +
				" and file_id = " + fileId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public List<Integer> listTerminalIds(long fileId, int gid) throws ExceptionCommonBase {
		String sql = "select terminal_id from t_terminal_game_file_link where 1 = 1 ";
		if(fileId>0){
			sql += " and file_id = " + fileId;
		}
		if(gid>0){
			sql += " and g_id = " + gid;
		}
		return this.getListInteger(sql);
	}

	public List<Integer> listTerminalIdsByGid(int gid) throws ExceptionCommonBase {
		String sql = "select terminal_id from t_terminal_game_file_link where 1 = 1 ";		
		if(gid>0){
			sql += " and g_id = " + gid;
		}
		return this.getListInteger(sql);
	}
	
	public List<Integer> listTerminalIds(List<Integer> ids) throws ExceptionCommonBase {
		if(ids != null && ids.size()>0){
			String sql = "select terminal_id from t_terminal where data_status = 1" +
					" and terminal_id in (" + Utils.toString(ids) + ")";
			return this.getListInteger(sql);
		}
		return new ArrayList<Integer>();
	}

	public List<Integer> listTerminalIdsByGroupId(int terminalGroupId) throws ExceptionCommonBase {
		String sql = "select terminal_id from t_terminal where terminal_group_id = "+terminalGroupId;
		return this.getListInteger(sql);
	}
}
